import { ref, computed } from "vue";
import { defineStore } from "pinia";
import { type ProfileItem } from "@/core3d/types/Libraries/Profiles";

export const useLibStore = defineStore("library", () => {
  const colors = ref<{ [key: string]: string }>({});
  const profiles = ref<ProfileItem[]>([]);

  const save_colors = (dict: { [key: string]: string }) => {
    //更新颜色
    for(let key in colors.value){
      if(key in dict){
        colors.value[key] = dict[key];
      }
    }

    localStorage.setItem("colors", JSON.stringify(colors.value));
  };

  const load_from_storage = () => {
    load_colors();
    load_profiles();
  };

  const load_colors = () => {
    var colorstring = localStorage.getItem("colors");
    if (colorstring) {
      colors.value = JSON.parse(colorstring);
    } else {
      var colordict = {};
      colors.value = colordict;
    }

    //添加默认的系统颜色
    if(!('wireframe' in colors.value)){
      colors.value['wireframe'] = 'rgb(0,0,0)';
    }
    if(!('error' in colors.value)){
      colors.value['error'] = 'rgb(255,0,0)';
    }
  };

  const load_profiles = () => {
    var profilesString = localStorage.getItem("profiles");
    if (profilesString) {
      profiles.value = JSON.parse(profilesString);
    } else {
      profiles.value = [];
      profiles.value.push({
        name: "45度斜门板",
        points: [
          { x: 0, y: 17 },
          { x: 20, y: 17 },
          { x: 20, y: 0 },
          { x: 0, y: 17 },
        ],
        lines: [{ type: 0 }, { type: 0 }, { type: 0 }, { type: 0 }],
      });
      profiles.value.push({
        name: "母拉手",
        points: [
          { x: 0, y: 0 },
          { x: 22, y: 0 },
          { x: 22, y: 37.5 },
          { x: 0, y: 37.5 },
          { x: 0, y: 0 },
        ],
        lines: [{ type: 0 }, { type: 0 }, { type: 0 }, { type: 0 }, { type: 0 }],
      });
      profiles.value.push({
        name:'15框玻璃门边框',
        points:[
          { x: 0, y: 0 },
          { x: 22, y: 0 },
          { x: 22, y: 17.5 },
          { x: 0, y: 17.5 },
          { x: 0, y: 0 },
        ],
        lines:[{ type: 0 }, { type: 0 }, { type: 0 }, { type: 0 }, { type: 0 }],
      });
      profiles.value.push({
        name: "铝框子拉手",
        points: [
          { x: 0, y: 0 },
          { x: 21.5, y: 0 },
          { x: 21.5, y: 20 },
          { x: 0, y: 20 },
          { x: 0, y: 0 },
        ],
        lines: [{ type: 0 }, { type: 0 }, { type: 0 }, { type: 0 }, { type: 0 }],
      });
      profiles.value.push({
        name: "铝框母拉手",
        points: [
          { x: 0, y: 0 },
          { x: 21.5, y: 0 },
          { x: 21.5, y: 42 },
          { x: 0, y: 42 },
          { x: 0, y: 0 },
        ],
        lines: [{ type: 0 }, { type: 0 }, { type: 0 }, { type: 0 }, { type: 0 }],
      });
      profiles.value.push({
        name: "15框玻璃门拉手",
        points: [
          { x: 0, y: 0 },
          { x: 25, y: 0 },
          { x: 25, y: 13 },
          { x: 0, y: 13 },
          { x: 0, y: 0 },
        ],
        lines: [{ type: 0 }, { type: 0 }, { type: 0 }, { type: 0 }, { type: 0 }],
      });
      profiles.value.push({
        name: "底板拉手",
        points: [
          { x: 0, y: 0 },
          { x: 19, y: 0 },
          { x: 19, y: 21 },
          { x: 0, y: 21 },
          { x: 0, y: 0 },
        ],
        lines: [{ type: 0 }, { type: 0 }, { type: 0 }, { type: 0 }, { type: 0 }],
      });
    }
  };

  return { colors, load_from_storage, save_colors, profiles };
});
